package cn.hairui.fastms.core.controller;


import cn.hairui.fastms.base.cache.CacheManage;
import cn.hairui.fastms.base.config.WebLogInfo;
import cn.hairui.fastms.constant.FieldConstant;
import cn.hairui.fastms.constant.GlobalConstant;
import cn.hairui.fastms.core.bean.SRole;
import cn.hairui.fastms.core.bean.SUser;
import cn.hairui.fastms.core.bean.SUserRole;
import cn.hairui.fastms.core.service.SRoleService;
import cn.hairui.fastms.core.service.SUserRoleService;
import cn.hairui.fastms.util.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户角色信息表 前端控制器
 * </p>
 *
 * @author lihairui
 * @since 2020-12-27
 */
@Controller
@RequestMapping("/sUserRole")
public class SUserRoleController {
    @Autowired
    private SUserRoleService sUserRoleService;
    @Autowired
    private SRoleService sRoleService;

    @WebLogInfo(description = "用户角色信息列表页面")
    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request) {
        return "pages/core/suserrole/suserrole";
    }

    @WebLogInfo(description = "用户角色信息新增页面")
    @GetMapping(value = "/add")
    public String add(HttpServletRequest request, Model model) {
        String userAccount = request.getParameter("userAccount");
        if (userAccount != null) {
            model.addAttribute("userAccount", userAccount);
            //通过userAccount查找未被授权角色的信息
            QueryWrapper<SUserRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(FieldConstant.USER_ACCOUNT[0], userAccount);
            List<SUserRole> sUserRoleList = sUserRoleService.list(queryWrapper);

            String[] roles = new String[sUserRoleList.size()];
            for (int i = 0; i < sUserRoleList.size(); i++) {
                roles[i] = sUserRoleList.get(i).getRoleCde();
            }
            QueryWrapper<SRole> queryWrapper2 = new QueryWrapper<>();
            if (sUserRoleList.size() > 0) {
                queryWrapper2.notIn(FieldConstant.ROLE_CDE[0], roles);
            }

            List<SRole> sRoleList = sRoleService.list(queryWrapper2);
            model.addAttribute("roles", sRoleList);
        }
        return "pages/core/suserrole/suserroleAdd";
    }

    @WebLogInfo(description = "用户角色信息列表数据获取")
    @RequestMapping(value = "/data")
    @ResponseBody
    public String data(HttpServletRequest request, Model model) {
        String pageSizeStr = request.getParameter("rows");
        String pageNumStr = request.getParameter("page");
        String userAccount = request.getParameter("userAccount");
        Integer pageNum = null;
        int pageSize = GlobalConstant.DEFAULT_PAGE_SIZE;
        if (pageNumStr != null) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSizeStr != null) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<SUserRole> queryWrapper = new QueryWrapper<>();

        if (userAccount != null && !"".equals(userAccount)) {
            queryWrapper.eq(FieldConstant.USER_ACCOUNT[0], userAccount);
        }

        /*List<SUserRole> sUserRoleList = sUserRoleService.list(queryWrapper);
        PageInfo<SUserRole> pageInfo = new PageInfo<>(sUserRoleList);
        return new TableResult(pageInfo.getTotal(), sUserRoleList);*/

       /* List<SRole> sRoleList = sUserRoleService.selectRolesByUserAccount(userAccount);
        PageInfo<SRole> pageInfo = new PageInfo<>(sRoleList);
        return new TableResult(pageInfo.getTotal(), sRoleList);*/

        List<SRole> sRoleList = sUserRoleService.selectRolesByUserAccount(userAccount);
        PageInfo<SRole> pageInfo = new PageInfo<>(sRoleList);
        List list = new ArrayList();
        for (SRole sRole : sRoleList) {
            Map map = new HashMap<>();
            map.put("roleCde", sRole.getRoleCde());
            map.put("roleName", sRole.getRoleName());
            map.put("userAccount", userAccount);
            list.add(map);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", list);
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject.toString();
    }

    @WebLogInfo(description = "用户角色信息数据保存")
    @RequestMapping(value = "/save")
    @ResponseBody
    public String save(HttpServletRequest request, String[] roleCde) {

        //String[] roleCde = request.getParameter("roleCde");
        String userAccount = request.getParameter("userAccount");
        SUser currentUser = (SUser) request.getSession().getAttribute(GlobalConstant.GLOBAL_SESSION_NAME);
        if (roleCde.length > 0) {
            for (int i = 0; i < roleCde.length; i++) {
                SUserRole sUserRole = new SUserRole();
                sUserRole.setRoleCde(roleCde[i]);
                sUserRole.setUserAccount(userAccount);
                sUserRole.setUpdateTime(DateUtil.getCurrentDateTimeStr());
                sUserRole.setUpdateUser(currentUser.getUserAccount());
                sUserRoleService.save(sUserRole);
            }
        }
        return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();

    }

    @WebLogInfo(description = "用户角色信息数据删除")
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Transactional
    public String delete(HttpServletRequest request, String userAccount, String roleCde) {
        QueryWrapper<SUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(FieldConstant.USER_ACCOUNT[0], userAccount).eq(FieldConstant.ROLE_CDE[0], roleCde);
        sUserRoleService.remove(queryWrapper);
        return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();
    }
}

